/**
 * Created by qsyan on 2016-12-4.
 */
import { takeEvery, takeLatest } from 'redux-saga'
import { call, put } from 'redux-saga/effects'
import 'isomorphic-fetch'

// worker Saga: will be fired on USER_FETCH_REQUESTED actions
function* fetchUser(action) {
    console.log('hehe....');
    try {
        const user = yield call(fetch('http://localhost:8080/Login/login').then(function (json,res){
            return Promise.reject(json)
        }));
        yield put({type: "USER_FETCH_SUCCEEDED", user: user});
    } catch (e) {
        console.log(e);
        yield put({type: "USER_FETCH_FAILED", message: e.message});
    }
}

/*
 Starts fetchUser on each dispatched `USER_FETCH_REQUESTED` action.
 Allows concurrent fetches of user.
 */
function* mySaga() {
    yield takeEvery("USER_FETCH_REQUESTED", fetchUser);
}

/*
 Alternatively you may use takeLatest.

 Does not allow concurrent fetches of user. If "USER_FETCH_REQUESTED" gets
 dispatched while a fetch is already pending, that pending fetch is cancelled
 and only the latest one will be run.
 */
function* mySaga() {
    yield takeLatest("USER_FETCH_REQUESTED", fetchUser);
}

export default mySaga;